package cn.myfirefly.moming.common.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.myfirefly.moming.common.dao.PositionDao;
import cn.myfirefly.moming.common.entity.MMPosition;
import cn.myfirefly.moming.common.model.PositionDto;
import cn.myfirefly.moming.common.model.PositionFilterDto;
import cn.myfirefly.core.Const;
import cn.myfirefly.core.model.PageResultDto;
import cn.myfirefly.utils.BeanUtil;
import cn.myfirefly.utils.StrUtil;

@Service
@Transactional
public class PositionService {
//	private Log logger = LogFactory.getLog(getClass());
	
	@Autowired
	private PositionDao positionDao;
	
	@SuppressWarnings("rawtypes")
	public PageResultDto getList(PositionFilterDto dto) {
		
		return positionDao.getList(dto);
	}
	
	@SuppressWarnings("rawtypes")
	public PageResultDto getList(PositionDto dto) {
		
		return positionDao.getList(dto);
	}
	
	public String save(PositionDto dto) {
		MMPosition position = null;
		// 更新
		if (StrUtil.notBlank(dto.getPositionSn())) {
			position = positionDao.get(dto.getPositionSn());
			if (position == null) {
				return "职位信息不存在，请刷新页面后重试。";
			}
			MMPosition tempPosition = positionDao.getPositionByCode(dto.getPositionCode());
			if (tempPosition != null && tempPosition.getPositionSn() != position.getPositionSn()) {
				return "职位编码已存在。";
			}
		} else {
		// 新增
			position = positionDao.getPositionByCode(dto.getPositionCode());
			if (position != null) {
				return "职位编码已存在。";
			} 
			position = new MMPosition();
			position.setStatus("0");
			position.setCreateBy(dto.getModifyBy());
			position.setCreateDate(dto.getModifyDate());
		}
		BeanUtil.copyBeans(dto, position);
		//Integer positionSn = 
		positionDao.save(position);
		
		return Const.ResultCode.SUCCESS;
	}
	
	public PositionDto getPosition(Integer positionSn) {
		MMPosition position = positionDao.get(positionSn);
		PositionDto positionDto = BeanUtil.copyBeans(PositionDto.class, position);
		return positionDto;
	}
	
	public String del(Integer positionSn) {
		MMPosition position = positionDao.get(positionSn);
		if (position == null) {
			return "职位信息不存在，请刷新页面后重试。";
		}
		positionDao.del(position);
		return Const.ResultCode.SUCCESS;
	}
	
	public String changeStatus(PositionDto dto) {
		MMPosition position = positionDao.get(dto.getPositionSn());
		if (position == null) {
			return "职位信息不存在，请刷新页面后重试。";
		}
		position.setStatus(dto.getStatus());
		position.setModifyBy(dto.getModifyBy());
		position.setModifyDate(dto.getModifyDate());
		positionDao.save(position);
		return Const.ResultCode.SUCCESS;
	}
	
}
